package com.hainatong.shiphelper.module.warehouse.fragment;


import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;

import com.hainatong.shiphelper.*;
import com.hainatong.shiphelper.databinding.FragmentRecyclerviewBinding;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;

import com.hainatong.shiphelper.module.warehouse.adapter.ShipWareHouseAdapter;
import com.hainatong.shiphelper.module.warehouse.entity.ShelfUuidBean;
import com.hainatong.shiphelper.module.warehouse.entity.ShipWareHouseBean;
import com.hainatong.shiphelper.module.warehouse.viewmodel.WareHouseViewModel;


import android.view.View;

import cn.bertsir.zbar.QrManager;
import cn.bertsir.zbar.Qr.ScanResult;
import com.hainatong.shiphelper.utils.GsonUtil;
import com.hainatong.shiphelper.utils.RecyclerViewHelper;
import com.hainatong.shiphelper.widget.LoadingDialog;
import com.hainatong.mvvmbase.rxbus.RxBus;
import com.hainatong.mvvmbase.rxbus.RxBusBaseMessage;
import io.reactivex.disposables.Disposable;


/**
 * 选择 仓库
 */
public class SelectWareHouseFragment extends MvvmLazyFragment<FragmentRecyclerviewBinding, WareHouseViewModel>
{
    // 1 - 入库 货架   2 - 入库 条码  3 - 出库 条码  4 - 归还 条码

    private ShipWareHouseAdapter adapter;

    private LoadingDialog loading;

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_recyclerview;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {

    }

    @Override
    protected void initView(View view)
    {
          loading = new LoadingDialog( getActivity() , getString( R.string.loading ) );
          adapter = new ShipWareHouseAdapter();
          RecyclerViewHelper.addLinerLayoutMgrNoDecoration(viewDataBinding.recyclerview);
          viewDataBinding.recyclerview.setAdapter(adapter);

          adapter.setOnItemClickListener((adapter1, view1, position) -> {
             // startScanQrcode(scanType);
            ShipWareHouseBean wareHouseBean = adapter.getItem( position );

            ComFragmentActivity.startViewByParam(getActivity(),Constants.VIEW_SELECT_SHELF, GsonUtil.obj2json(wareHouseBean));

          });

        Disposable disposable = RxBus.getDefault().toObservable(Constants.COM_ATY_RXMSG_CODE , RxBusBaseMessage.class)
                                .subscribe( rxMsg ->{
                                    if( rxMsg.getCode() == Constants.WH_SUB_SCAN_CODE )
                                    {
                                        //扫码   货架 uuid
                                        startScanQrcode(1);

                                    }
                                } );
        addSubscription( disposable );
    }

    @Override
    protected void lazyLoad()
    {
        //先根据 船 查 仓库
        viewModel.queryWareHouseByShipId( GlobalConfig.getInstance().getUserInfo().getShip_id() );
        viewModel.getShipWareHouseResult().observe( this , result -> {

            if( result.isSuccess() )
            {
                if(  result.getData() != null && !result.getData().isEmpty()  )
                {
                    adapter.setList( result.getData() );
                }
            }
            else
            {
                ToastUtils.showShort( result.getMsg() );
            }
        } );
        //扫码根据货架ID   拿到 货架信息
        viewModel.getShelfUuidResult().observe( this , result -> {
            loading.hide();
            if( result.isSuccess() )
            {
                if( result.getData() != null )
                {
                    // 跳转 入库  货架
                    ShelfUuidBean shelfUuid = result.getData();
                    ComFragmentActivity.startViewByParam(  getActivity(),Constants.VIEW_TYPE_RUKU ,GsonUtil.obj2json( shelfUuid ) );
                }
            }
            else
            {
                ToastUtils.showShort( result.getMsg() );
            }
        } );

    }

    public void startScanQrcode(int type )
    {
        QrManager.getInstance().init( App.getScanQrConfigByType( type ) ).startScan(getActivity(), new QrManager.OnScanResultCallback() {
            @Override
            public void onScanSuccess(ScanResult result)
            {
                // 货架 二维码 uuid
                if( result.getType() == ScanResult.CODE_QR )
                {
                    // 跳转 入库  货架
                    //发请求 获取 货架信息
                    loading.show();
                    viewModel.queryWareHouseShelfInfoByUUid( result.getContent() );
                }
                LogUtils.e(TAG, "货架 UUID: " + result.getContent());
                   /*
                switch ( 1 )
                {

                    case 2:
                        break;
                    case 3:
                        if( result.getType() == ScanResult.CODE_BAR)
                        {
                            // 跳转 出库
                            Bundle bundle = new Bundle();
                            bundle.putString( Constants.FRAGMNET_TYPE, Constants.VIEW_TYPE_CHUKU);
                            bundle.putString( Constants.SCAN_CONTENT_CODE,  ""+result.getContent() );
                            ComFragmentActivity.start( getActivity(),bundle );
                        }
                        else
                        {
                            ToastUtils.showShort( result.getContent() );
                        }
                        break;
                    case 4:
                        if( result.getType() == ScanResult.CODE_BAR)
                        {
                            //ToastUtils.showShort( result.getContent() );
                            // 跳转 归还
                            Bundle bundle = new Bundle();
                            bundle.putString( Constants.FRAGMNET_TYPE, Constants.VIEW_TYPE_RETURN);
                            bundle.putString( Constants.SCAN_CONTENT_CODE,  ""+result.getContent() );
                            ComFragmentActivity.start( getActivity(),bundle );
                        }
                        else
                        {
                            ToastUtils.showShort( result.getContent() );
                        }
                        break;
                         }
                        */

            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        loading.dismiss();
    }
}
